/**
 * 
 */
package com.java.test.system.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.test.system.dto.LogInfo;
import com.java.test.system.dto.LoginAccountInfo;
import com.java.test.system.dto.MenuInfo;
import com.java.test.system.dto.PowerInfo;
import com.java.test.system.dto.RoleInfo;
import com.java.test.system.mapper.LogInfoMapper;
import com.java.test.system.mapper.LoginAccountMapper;
import com.java.test.system.mapper.MenuInfoMapper;
import com.java.test.system.mapper.RoleMapper;
import com.java.test.system.service.SystemManageService;
import com.java.test.util.MenuType;
import com.java.test.util.PageInfo;

/**
 * @author Administrator
 *
 */
@Service
public class SystemManageServiceImpl implements SystemManageService {

	@Autowired
	LoginAccountMapper loginAccountMapper;

	@Autowired
	RoleMapper roleMapper;

	@Autowired
	MenuInfoMapper menuInfoMapper;

	@Autowired
	LogInfoMapper logInfoMapper;

	@Override
	public PageInfo<LoginAccountInfo> selectaccountList(PageInfo<LoginAccountInfo> page) {
		LoginAccountInfo info = page.getInfo();

		int total = loginAccountMapper.selectloginaccountCount(info);

		List<LoginAccountInfo> list = loginAccountMapper.selectloginaccountList(info, page);

		list.forEach(b -> {
			int id = b.getRoleId();
			if (id == 0)
				return;
			RoleInfo item = new RoleInfo();
			item.setId(id);
			RoleInfo temp = roleMapper.selectrolebyId(item);
			b.setRoleName(temp == null ? null : temp.getRoleName());
		});

		page.setTotalCount(total);

		page.setList(list);

		return page;
	}

	@Override
	public int insertloginaccountInfo(LoginAccountInfo info) {
		info.setCreateTime(LocalDateTime.now());
		return loginAccountMapper.insertloginaccountInfo(info);
	}

	@Override
	public int deleteloginaccountInfo(int[] ids) {

		return loginAccountMapper.deleteloginaccountInfo(ids);
	}

	@Override
	public int updateloginaccountInfouse(int[] ids) {
		return loginAccountMapper.updateloginaccountInfouse(ids);
	}

	@Override
	public int updateloginaccountInfonotuser(int[] ids) {
		return loginAccountMapper.updateloginaccountInfonotuser(ids);
	}

	@Override
	public int updateloginaccountPassword(LoginAccountInfo info) {
		return loginAccountMapper.updateloginaccountPassword(info);
	}

	@Override
	public List<RoleInfo> selectroleList(RoleInfo info) {
		return roleMapper.selectroleList(info);
	}

	@Override
	public int insertroleInfo(RoleInfo info) {
		return roleMapper.insertroleInfo(info);
	}

	@Override
	public RoleInfo selectrolebyId(RoleInfo info) {
		return roleMapper.selectrolebyId(info);
	}

	@Override
	public int updateroleInfo(RoleInfo info) {
		return roleMapper.updateroleInfo(info);
	}

	@Override
	public int deleteroleInfo(RoleInfo info) {
		return roleMapper.deleteroleInfo(info);
	}

	@Override
	public PowerInfo selectallMenuList() {

		PowerInfo info = new PowerInfo();

		List<MenuInfo> list1 = menuInfoMapper.selectMenueList(MenuType.SYGL.getCode());

		List<MenuInfo> list2 = menuInfoMapper.selectMenueList(MenuType.HYGL.getCode());

		List<MenuInfo> list3 = menuInfoMapper.selectMenueList(MenuType.SHGL.getCode());

		List<MenuInfo> list4 = menuInfoMapper.selectMenueList(MenuType.SJGL.getCode());

		List<MenuInfo> list5 = menuInfoMapper.selectMenueList(MenuType.PTGL.getCode());

		List<MenuInfo> list6 = menuInfoMapper.selectMenueList(MenuType.ZFGL.getCode());

		List<MenuInfo> list7 = menuInfoMapper.selectMenueList(MenuType.HDLB.getCode());

		List<MenuInfo> list8 = menuInfoMapper.selectMenueList(MenuType.XXZX.getCode());

		List<MenuInfo> list9 = menuInfoMapper.selectMenueList(MenuType.ZDQDGL.getCode());

		List<MenuInfo> list10 = menuInfoMapper.selectMenueList(MenuType.XTGL.getCode());

		List<MenuInfo> list11 = menuInfoMapper.selectMenueList(MenuType.DLGL.getCode());

		List<MenuInfo> list12 = menuInfoMapper.selectMenueList(MenuType.TZGL.getCode());
		
		info.setCount1(list1);
		info.setCount2(list2);
		info.setCount3(list3);
		info.setCount4(list4);
		info.setCount5(list5);
		info.setCount6(list6);
		info.setCount7(list7);
		info.setCount8(list8);
		info.setCount9(list9);
		info.setCount10(list10);
		info.setCount11(list11);
		info.setCount12(list12);

		return info;
	}

	@Override
	public LoginAccountInfo loginuser(String userName, String password) {
		return loginAccountMapper.loginuser(userName, password);
	}

	@Override
	public LoginAccountInfo loginuserisexits(String userName) {
		return loginAccountMapper.loginuserisexits(userName);
	}

	@Override
	public List<MenuInfo> selectloginuserMenu(int roleId) {

		RoleInfo srole = new RoleInfo();
		srole.setId(roleId);

		RoleInfo roleinfo = roleMapper.selectrolebyId(srole);

		if (roleinfo == null) {
			return null;
		}
		String powerStr = roleinfo.getPower();

		if (powerStr == null || "".equals(powerStr)) {
			return null;
		}

		List<MenuInfo> umenuList = menuInfoMapper.selectrolemenueListbyrole(powerStr);

		// 查询出所有子菜单 ，对子菜单进行处理
		if (umenuList == null || umenuList.size() <= 0)
			return null;

		Map<Integer, List<MenuInfo>> groupMap = umenuList.stream()
				.collect(Collectors.groupingBy(MenuInfo::getParentid));

		List<MenuInfo> childparent = new ArrayList<MenuInfo>();

		groupMap.forEach((key, value) -> {

			MenuInfo info = menuInfoMapper.selectmenueInfobyid(key);
			if (info == null)
				return;
			info.setChildren(value);
			childparent.add(info);

		});

		return childparent;
	}

	@Override
	public PageInfo<LogInfo> selectlogList(PageInfo<LogInfo> page) {
		LogInfo info = page.getInfo();

		int total = logInfoMapper.selectoperlogCount(info);

		List<LogInfo> list = logInfoMapper.selectoperlogList(info, page);

		page.setTotalCount(total);

		page.setList(list);

		return page;
	}

	@Override
	public int insertlogInfo(String loginaccount, String ip, String msgcontent) {
		LogInfo loginfo = new LogInfo(loginaccount, ip, LocalDateTime.now(), msgcontent);
		return logInfoMapper.insertlogInfo(loginfo);
	}

}
